// pages/productDetail/productDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    no_pic:'/static/images/no-pic.png',
    defalut_face:'/static/defalut_face.png',
    info:{detail:{name:''},gg:[],collected:0,pj:[]},
    video:'',
    id:0,
    gg_id:0,
    height: 64, //header高度
    top: 26, //标题图标距离顶部距离
    scrollH: 0, //滚动总高度
    opcity: 0,
    iconOpcity: 0.5,
    banner: [],
    bannerIndex: 0,
    value: 1,
    videoRef:null,
    isPlaying:false,
    showPlayBtn:true
  },
  onLoad: function(options) {
    console.log(options)
    this.setData({
      id:options.id
    })
    let obj = {};
    obj = wx.getMenuButtonBoundingClientRect();
    setTimeout(() => {
      wx.getSystemInfo({
        success: res => {
          let width = obj.left || res.windowWidth;
          let height = obj.top ? obj.top + obj.height + 8 : res.statusBarHeight + 44;
          let top = obj.top ? obj.top + (obj.height - 32) / 2 : res.statusBarHeight + 6;
          let scrollH = res.windowWidth;
          this.setData({
            width: width,
            height: height,
            top: top,
            scrollH: scrollH
          });
        }
      });
      this.load();
    }, 0);
  },
  videoErrorCallback(e){
				
  },
  async load(){
    let res=await wx.util.post("item/index",{id:this.data.id});
    this.setData({
      banner:res.data.banner
    })
    if(this.data.banner.length==0){
      this.setData({
        banner:[this.data.no_pic]
      })
    }
    if(res.data.video){
      this.setData({
        video:res.data.video,
        videoRef:wx.createVideoContext("itemVideo")
      })
    }
    this.setData({
      info:res.data,
      gg_id:res.data.gg_id
    })
  },
  async collecting() {
    let res=await wx.util.post("user/collectItems",{id:this.data.id});
    this.setData({
      'info.collected':res.data
    })
  },
  setGg(e){
    let item=e.currentTarget.dataset.item;
    let price=(item.price_vip+'').split('.');
    this.setData({
      gg_id:item.hash,
      'info.price':item.price,
      'info.price1':price[0],
      'info.price2':"."+price[1],
    })
  },
  toCard(e){
    let id=e.currentTarget.dataset.id;
    if(id==0){
      wx.util.href('/pages/card/card');
      return;
    }
    wx.util.href('/pages/card/detail?id='+id);
  },
  goHome(){
    wx.switchTab({
      url:"/pages/index/index"
    })
  },
  bannerChange: function(e) {
    this.setData({
      bannerIndex:e.detail.current
    })
    if(!this.data.video){
      return ;
    }
    if(this.data.bannerIndex>0){
      this.setData({
        showPlayBtn:true
      })
      return;
    }
    this.setData({
      showPlayBtn:!this.data.isPlaying
    })
  },
  previewImage: function(e) {
    let index = e.currentTarget.dataset.index;
    wx.previewImage({
      current: this.data.banner[index],
      urls: this.data.banner
    });
  },
  back: function() {
    wx.navigateBack({
      fail:()=>{
        wx.switchTab({
          url:'/pages/index/index'
        })
      }
    });
  },
  change: function(e) {
    this.setData({
      value:e.value
    })
  },
  
  evaluate(){
    wx.util.href('../goodsEvaluate/goodsEvaluate?id='+this.data.id)
  },
  submit() {
    wx.util.href('../submitOrder/submitOrder?gg_id='+this.data.gg_id,true);
  },
  shop(){
    wx.util.href('../shop/shop')
  },
  play(){
    if(!this.data.videoRef){
      return;
    }
    this.setData({
      bannerIndex:0
      ,showPlayBtn:false
      ,isPlaying:true
    })
    this.data.videoRef.play();
  },
  onShareAppMessage: function (e) {
    let wxid=wx.getStorageSync("openid") || '';
      return {
      title: this.data.info.detail.name,
      path: '/pages/productDetail/productDetail?id='+this.data.id+'&wxid='+wxid,
    }
  },
  onShareTimeline: function (e) {
    let wxid=wx.getStorageSync("openid") || '';
      return {
      title: this.data.info.detail.name,
      query: 'id='+this.data.id+'&wxid='+wxid,
    }
  },
  onPageScroll(e) {
    let scroll = e.scrollTop <= 0 ? 0 : e.scrollTop;
    let opcity = scroll / this.data.scrollH;
    if (this.data.opcity >= 1 && opcity >= 1) {
      return;
    }
    this.setData({
      opcity:opcity
      ,iconOpcity:0.5 * (1 - opcity < 0 ? 0 : 1 - opcity)
    })
  }

})